using UnityEngine;
using System.Collections;

public class AlienStatsUI : MonoBehaviour {
	
	public GUISkin mainSkin;
	
	public AlienGenome target;
	public float shiftX=0.5f;
	public float shiftY=0.5f;
	// Use this for initialization
	void Start () {
	
	}
	
	// Update is called once per frame
	void Update () {
		if(rotate)
			this.transform.Rotate(0, 30f * Time.deltaTime, 0);
		//else if (this.transform.localRotation.eulerAngles[2]
	}
	
	void OnGUI() {
		GUI.skin= mainSkin;
		if(target != null && target.gameObject.active) {
			Vector3 worldPoint= this.transform.position;
				worldPoint.y+=shiftY;
				worldPoint.x+=shiftX;
			Vector2 screenCoord= Camera.mainCamera.WorldToViewportPoint (worldPoint);
		
			if( mainSkin.GetStyle("Stats") != null)
				GUI.Label(new Rect(screenCoord.x * (float)Screen.width, (1-screenCoord.y)* (float)Screen.height, 150, 200), target.ToString(), mainSkin.GetStyle("Stats"));
			else
				GUI.Label(new Rect(screenCoord.x * (float)Screen.width, (1-screenCoord.y)* (float)Screen.height, 150, 200), target.ToString());
		}
	}
	
	public bool rotate= false;
	void OnMouseOver() {
		Debug.Log("OnMoouseOver");
		bool rotate=true;		
	}
	
	void OnMouseExit() {
		bool rotate=false;
		
	}
	
	public void attachAlien( AlienGenome alien) {
		ClearGore();
		//alien.transform.Translate( this.gameObject.transform.position - alien.transform.position );
		alien.transform.parent= this.gameObject.transform;		
		alien.transform.localPosition= Vector3.zero;
		alien.transform.localEulerAngles= Vector3.zero;
		alien.transform.LookAt(Camera.main.transform);
		this.target= alien.GetComponent<AlienGenome>();
		
		this.transform.localEulerAngles= Vector3.zero;
		alien.transform.localScale= new Vector3(1f,1f,1f);
	}
	
	public void ClearGore() {
		foreach(Transform child in transform){ 
			Destroy(child.transform.gameObject);	
		}	
	}
	
//	public void OnDisable() {
//		foreach(Transform child in transform){ 
//			Destroy(transform.gameObject);	
//		}
//	}
}

